//
//  LLStatusViewModel.swift
//  新浪微博
//
//  Created by mac on 16/5/28.
//  Copyright © 2016年 mac.com. All rights reserved.
//

import UIKit

class LLStatusViewModel: NSObject {
    var status:LLStatus?
    //认证类型的图片
    var veritiedImage: UIImage?
    var mbrankImage: UIImage?
    var headImageURL: NSURL?
    //微博来源
    var sourceText: String?
    
    var repostsText: String?
    
    var commentsText: String?
    
    var attitudesText: String?
    //处理时间
    var time: String?{
            return NSDate.sinaDate(status?.created_at ?? "")?.fullDescription()
           }
    init(status:LLStatus) {
        super.init()
        self.status = status
        headImageURL = NSURL(string: status.user?.profile_image_url ?? "")
        //处理认证类型的图片
        dealVeitiedImage()
        //处理用户等级的图片
        dealMbrankImage()
        dealsource()
//        time = NSDate.sinaData(status.created_at ?? "")?.fullDescription()

        dealSourceTime()
        commentsText = dealToolBarText(status.comments_count, defaultText: "评论")
        repostsText = dealToolBarText(status.reposts_count, defaultText: "转发")
        attitudesText = dealToolBarText(status.attitudes_count, defaultText: "赞")


    }
}
extension LLStatusViewModel{
   private func dealVeitiedImage(){
    
        let veriteidType =  status?.user?.verified ?? 0
        if veriteidType == 1
            {
         veritiedImage = UIImage(named: "avatar_vip")
        }
        
    }
    //处理微博来源
    func dealsource(){
        let startFlag = "\">"
        let endFlag = "</a>"
        let str = status?.source ?? ""
        let startRang = str.rangeOfString(startFlag)
        let endRange = str.rangeOfString(endFlag)
        if let start = startRang, end = endRange{
        let range = start.endIndex..<end.startIndex
            sourceText = str.substringWithRange(range)
        }
        
        
    }
    //处理时间
    private func dealSourceTime(){
    
    
    }
    //在这个方法计算底部工具条显示的文字
    //如果没有评论或者转发或者赞  就显示默认文字
    //- 有相关的数据 就显示对应的数据,但是如果超过一万条 就应该显示多少万条
    func dealToolBarText(count: Int,defaultText: String) -> String {
        
        //let count = 11000
        if count <= 0 {
            return defaultText
        }
        //大于一万
        if count >= 1000 {
            return "\(Double(count / 1000))千"
        }
        return "\(count)"
    }
    

   private func dealMbrankImage(){
        //用户等级
        let mbType = status?.user?.mbrank ?? 0
        if mbType > 0 && mbType < 7 {
            // 1 ~ 6
            //转译会自动加上 optional
            let image = UIImage(named: "common_icon_membership_level\(status?.user?.mbrank ?? 0)")
            mbrankImage = image
        }
        

    
    }

}